  ### Robustness: Supply of friends

ind_controls <- paste("male", "factor(religion_simple)", 
                      "age_grade_distortion", "score_self_esteem", 
                      "score_parents_support", "score_study", sep = "+")

ses_controls <- paste("scores_poverty", "score_neighborhood_quality", 
                      "factor(occ_father_simple_no_job_no_na)",
                      "factor(occ_mother_simple_no_job_no_na)", sep = "+")

class_fe <- paste("factor(class_id)")
  
school_fe <- paste("factor(school_id)")

classroom_char <- paste("sh_nw_class", "avg_score_racism_class", "class_size"
                        , "avg_score_climate_school_class" , "avg_score_homophobia_class" , "avg_anti_equality_score_class"
                        , "high_school" , "avg_dominance_score_class" , "avg_scores_poverty_class" 
                        , "avg_score_violence_school_class" 
                        , "avg_score_parents_support_class" , "avg_score_self_esteem_class" , "avg_score_study_class"
                        , "EDFIS_missing" , "sh_missing_interview_class" , "avg_score_neighborhood_quality_class"
                        , sep = "+")


regs_1_control <- function(dep_var, race_var, grade_var, control_var) {
  # dep_var = "ssi_same_negro_na_2_std"
  # race_var = "factor(negro_na)"
  # grade_var = "grades_score"
  # control_var = "skin_color"
  
  
  # basic regression
  basic_reg <- paste0(dep_var, "~", paste0(race_var,"*", grade_var), "+", control_var)
  
  Form_basic <- formula(basic_reg)
  
  # Individual control
  Form_ind <- formula(paste(basic_reg, ind_controls, sep = "+"))
  
  # SES control
  Form_ses <- formula(paste(basic_reg, ses_controls, sep = "+"))
  
  # Individual + SES control
  Form_ind_ses <- formula(paste(basic_reg, ind_controls, 
                                ses_controls, sep = "+"))
  
  # Individual controls + Classroom FE
  Form_ind_class_fe <- formula(paste(basic_reg, ind_controls, 
                                     class_fe, sep = "+"))
  
  # SES controls + Classroom FE
  Form_ses_class_fe <- formula(paste(basic_reg, ses_controls, 
                                     class_fe, sep = "+"))
  
  # Ind. + SES controls + Classroom FE
  Form_ind_ses_class_fe <- formula(paste(basic_reg, ind_controls,
                                         ses_controls, class_fe, 
                                         sep = "+"))
  
  # Ind. + SES controls + Classroom Char + School FE
  Form_ind_ses_school_fe <- formula(paste(basic_reg, ind_controls,
                                          ses_controls, school_fe, 
                                          classroom_char, 
                                          sep = "+"))
  
  list_specifications <- c(Form_basic, Form_ind, Form_ses, Form_ind_ses,
                           Form_ind_class_fe, Form_ses_class_fe, 
                           Form_ind_ses_class_fe, Form_ind_ses_school_fe) 
  
  results <- lapply(list_specifications, function(x) (lm(x, data = data, subset = (race < 4))))
  results_r <- lapply(results, robust_se, "HC3")
  results_c <- lapply(results, cluster_se, "class_id")
  
  results_final <- list(results, results_r, results_c)  
  return(results_final)
}


cor(cbind(data$supply_2_sd_same_race_fr_2, data$grades_score, 
          data$grades_score_square, data$ssi_other_race_negro_na_2_std,
          data$ssi_same_negro_na_2_std, data$ssi_all_race_2_std),
    use = 'pairwise.complete')

select <- which(data$negro_na == 0)

cor(cbind(data$supply_2_sd_same_race_fr_2[select], data$grades_score[select], 
          data$grades_score_square[select], data$ssi_other_race_negro_na_2_std[select],
          data$ssi_same_negro_na_2_std[select], data$ssi_all_race_2_std[select]),
    use = 'pairwise.complete')


supply_friends <- paste("supply_2_sd_same_race_fr_2")

supply_class_friends <-  paste("n_black_first_4", "n_black_second_4", "n_black_third_4", "n_black_fourth_4",
                               "n_brown_first_4", "n_brown_second_4", "n_brown_third_4", 
                               "n_brown_fourth_4", "n_white_first_4", "n_white_second_4",
                               "n_white_third_4", "n_white_fourth_4", sep = "+") 


reg_same_supply_ind <- regs_1_control(dep_var = "ssi_same_negro_na_2_std", 
                                      race_var = "negro_na", 
                                      grade_var = "grades_score",
                                      control_var = supply_friends)

reg_all_supply_ind <- regs_1_control(dep_var = "ssi_all_race_2_std", 
                                     race_var = "negro_na", 
                                     grade_var = "grades_score",
                                     control_var = supply_friends)

reg_other_supply_ind <- regs_1_control(dep_var = "ssi_other_race_negro_na_2_std", 
                                       race_var = "negro_na", 
                                       grade_var = "grades_score",
                                       control_var = supply_friends)

reg_same_supply_class <- regs_1_control(dep_var = "ssi_same_negro_na_2_std", 
                                        race_var = "negro_na", 
                                        grade_var = "grades_score",
                                        control_var = supply_class_friends)

reg_all_supply_class <- regs_1_control(dep_var = "ssi_all_race_2_std", 
                                       race_var = "negro_na", 
                                       grade_var = "grades_score",
                                       control_var = supply_class_friends)

reg_other_supply_class <- regs_1_control(dep_var = "ssi_other_race_negro_na_2_std", 
                                         race_var = "negro_na", 
                                         grade_var = "grades_score",
                                         control_var = supply_class_friends)

list_regs_c <- list(reg_same_supply_ind[[3]][[7]], reg_same_supply_class[[3]][[8]],
                    reg_other_supply_ind[[3]][[7]], reg_other_supply_class[[3]][[8]],
                    reg_all_supply_ind[[3]][[7]], reg_all_supply_class[[3]][[8]])

list_regs <- list(reg_same_supply_ind[[1]][[7]], reg_same_supply_class[[1]][[8]],
                  reg_other_supply_ind[[1]][[7]], reg_other_supply_class[[1]][[8]],
                  reg_all_supply_ind[[1]][[7]], reg_all_supply_class[[1]][[8]])

n_obs <- sapply(list_regs, get_obs)


r_adj <- sapply(list_regs, get_adj_r)

variables_to_omit <- "(age)|(male)|(religion)|(class_id)|(avg_)|
                      |(class)|(school)|(missing)|(EM)|(occ_)|(score_)|
                      |(poverty)|(Constant)|(supply)|(occ_pai_)|(n_)"

cov_var_labels_bi <- c("Nonwhite", "Grades", "Nonwhite*Grades")

## Table S13
stargazer(list_regs_c, 
          column.separate = c(2,2,2),
          covariate.labels = ,
          dep.var.labels.include = F,
          dep.var.caption = "Social status",
          column.labels = c("Same race", "Other races", "All races"),
          omit = variables_to_omit, 
          omit.stat = c("f", "rsq","ser"),
          add.lines = list(c("Supply of friends", "Individual", "Class", "Individual", "Class", "Individual", "Class"),
                           c("Observation", n_obs),
                           c("Adjusted R2", r_adj)),
          column.sep.width = "0pt", 
          font.size = "tiny", type = "text")


stargazer(list_regs_c, 
          column.separate = c(2,2,2),
          covariate.labels = cov_var_labels_bi,
          dep.var.labels.include = F,
          dep.var.caption = "Social status",
          column.labels = c("Same race", "Other races", "All races"),
          omit = variables_to_omit, 
          omit.stat = c("f", "rsq","ser"),
          add.lines = list(c("Supply of friends", "Individual", "Class", "Individual", "Class", "Individual", "Class"),
                           c("Observation", n_obs),
                           c("Adjusted R2", r_adj)),
          column.sep.width = "-5pt", 
          font.size = "footnotesize", type = "latex", 
          out = "tables/regs_1_supply.tex",
          label = "regs_1_supply",
          title = "Robustness of estimates to controls for supply of potential friends - Linear specification")

